package com.whccb.apps.office.document.at;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.actionsoft.bpms.commons.at.AbstExpression;
import com.actionsoft.bpms.commons.at.ExpressionContext;
import com.actionsoft.bpms.commons.database.RowMapper;
import com.actionsoft.bpms.org.cache.DepartmentCache;
import com.actionsoft.bpms.org.cache.RoleCache;
import com.actionsoft.bpms.org.cache.UserMapCache;
import com.actionsoft.bpms.org.model.DepartmentModel;
import com.actionsoft.bpms.org.model.UserMapModel;
import com.actionsoft.bpms.util.ConsolePrinter;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.exception.AWSExpressionException;
import com.actionsoft.sdk.local.SDK;

public class DocDWLimits extends AbstExpression {
    private ExpressionContext at;

    public DocDWLimits(ExpressionContext atContext, String expressionValue) {
        super(atContext, expressionValue);
        // TODO Auto-generated constructor stub
        at = atContext;
    }

    @Override
    public String execute(String arg) throws AWSExpressionException {
        // TODO Auto-generated method stub
        String boname = getParameter(arg, 1).trim();
        String field = getParameter(arg, 2).trim();
        String uid = getParameter(arg, 4).trim();
        String rolename = getParameter(arg, 3).trim();
        if ("admin".equals(at.getUserContext().getUID())) {
            return "1=1";
        }
        if (StringUtils.isBlank(boname) || StringUtils.isBlank(field)) {
            return "1=2";
        }
        if (StringUtils.isBlank(uid)) {
            uid = at.getUserContext().getUID();
        }
        String result = "";
        String[] fields = field.split("-");

        List<UserMapModel> umms = UserMapCache.getMapListOfUser(uid);
        if (StringUtils.isNotBlank(rolename)) {
            String dept = "";
            for (UserMapModel umm : umms) {
                if ("业务管理员".equals(
                        RoleCache.getModel(umm.getRoleId()).getName())) {
                    return " 1=1";
                }
                if (rolename.equals(
                        RoleCache.getModel(umm.getRoleId()).getName())) {

                    String deptid = SDK.getRuleAPI().executeAtScript(
                            "@getAwsDeptIdByOuterId(@getUserExtInfo(" + uid
                                    + ",HRFIRSTORGID))");
                    if (StringUtils.isNotBlank(deptid)
                            && DepartmentCache.getModel(deptid) != null) {
                        List<DepartmentModel> listdm = new ArrayList<DepartmentModel>();
                        DepartmentCache.getAllDepartmentListOrder(deptid,
                                listdm);
                        listdm.add(DepartmentCache.getModel(deptid));

                        if (!listdm.isEmpty() && listdm.size() > 0) {
                            for (DepartmentModel dm : listdm) {
                                if (StringUtils.isBlank(dept)) {
                                    dept = "'" + dm.getId() + "'";
                                } else {
                                    dept += ",'" + dm.getId() + "'";
                                }
                            }
                            result = " USERDEPTID IN (" + dept + ") ";
                            break;
                        }
                    }
                }
            }
        }

        String sql = "SELECT TEAMID FROM ORGTEAM_MEMBER WHERE USERID=:USERID ";
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("USERID", uid);
        List<Map<String, String>> teamids = DBSql.query(sql, paraMap,
                new RowMapper<Map<String, String>>() {

                    @Override
                    public Map<String, String> mapRow(ResultSet arg0, int arg1)
                            throws SQLException {
                        // TODO Auto-generated method stub
                        Map<String, String> map = new HashMap<String, String>();
                        map.put("TEAMID", arg0.getString("TEAMID"));
                        return map;
                    }
                });
        if (teamids != null && !teamids.isEmpty() && teamids.size() > 0) {
            String teama = "";
            for (Map<String, String> teamid : teamids) {
                String team = teamid.get("TEAMID") + "";
                if (StringUtils.isBlank(team)) {
                    continue;
                }
                for (int i = 0; i < fields.length; i++) {
                    if (StringUtils.isBlank(teama)) {
                        teama = fields[i] + " LIKE '%" + team + "%' ";
                    } else {
                        teama += " OR " + fields[i] + " LIKE '%" + team + "%' ";
                    }
                }
            }
            if (StringUtils.isBlank(result)) {
                result = teama;
            } else {
                result += " OR " + teama;
            }
        }
        ConsolePrinter.info("发文查询SQL" + result);
        ConsolePrinter.info("发文查询SQL" + result);
        if (StringUtils.isBlank(result)) {
            result = " 1=2 ";
        }
        return result;
    }

}
